package com.blackrock.das.client;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.blackrock.das.service.DataInitialiser;

/**
 * The Class DbInitialiserClient.
 */
public class DbInitialiserClient {

    /** The Constant LOGGER. */
    private final static Logger LOGGER = Logger.getLogger(DbInitialiserClient.class);

    /** The Constant CMD_LOAD. */
    private static final String CMD_LOAD = "loadMock";

    /** The Constant CMD_DROP. */
    private static final String CMD_DROP = "drop";

    /**
     * The main method.
     *
     * @param args the arguments
     */
    @SuppressWarnings("static-access")
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "spring/spring.xml" }, true);
        DataInitialiser dataInitialiser = applicationContext.getBean(DataInitialiser.class);
        PosixParser parser = new PosixParser();
        Options options = new Options();
        options.addOption(OptionBuilder.withArgName(CMD_DROP).withDescription("Drop an existing key space and recreate it.").create("d"));
        options.addOption(OptionBuilder.withArgName(CMD_LOAD).withDescription("Load mock data.").create("l"));
        try {
            CommandLine commandLine = parser.parse(options, args);
            if (commandLine.hasOption("d")) {
                LOGGER.info("COMMAND SELECTED: Recreate the key space");
                dataInitialiser.regenerateKeySpace();
            }
            if (commandLine.hasOption("l")) {
                LOGGER.info("COMMAND SELECTED: Load mock data");
                dataInitialiser.generateMockData();
            }
            applicationContext.close();
        } catch (ParseException e) {
            HelpFormatter formatter = new HelpFormatter();
            System.err.println(e.getLocalizedMessage());
            formatter.printHelp("java DbInitialize", options);
            applicationContext.close();
        }
    }
}
